//
//  OASelectPersonSecondController.m
//  OASystem
//
//  Created by zhoupengfei on 15/5/3.
//  Copyright (c) 2015年 zpf. All rights reserved.
//

#import "OASelectPersonSecondController.h"
#import "OANameListModel.h"
#import "OASubNameListModel.h"
#import "HeaderView.h"
#import "HttpTools.h"
#import "MBProgressHUD.h"
#import "GDataDefines.h"
#import "Toast+UIView.h"
#import "OACheckBoxFootView.h"
@interface OASelectPersonSecondController ()<HeaderViewDelegate,HeaderViewDelegate2>
@property(nonatomic,weak)OACheckBoxFootView * checkBoxFootView;
@property(nonatomic,strong)UIBarButtonItem * rightItem;
@end

@implementation OASelectPersonSecondController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupRightItems];
    OACheckBoxFootView * checkBoxFootView = [OACheckBoxFootView checkBoxFootView];
    self.tableView.tableFooterView = checkBoxFootView;
    self.checkBoxFootView = checkBoxFootView;
        _operationItem.enabled = YES;

}

-(void)setupRightItems{
    _operationItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(operationACtion)];
    self.navigationItem.rightBarButtonItem = _operationItem;
    
}

-(void)operationACtion{
_operationItem.enabled = NO;

    NSMutableArray * tempArray = [NSMutableArray array];
    for (OANameListModel * nameLisModel in self.groupArray) {
        for (OASubNameListModel * subModel in nameLisModel.subArray) {
            if (subModel.isSelected) {
                [tempArray addObject:subModel.value];
                
            }
        }
    }
    if (tempArray.count == 0) {
        return;
    }
     [MBProgressHUD showHUDAddedTo:self.navigationController.view  animated:YES];
    self.rightItem.enabled = NO;
    NSString * selectPersons = [tempArray componentsJoinedByString:@","];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":self.fileId},@{@"userId":userId},@{@"segment":self.setpString},@{@"staff":selectPersons},@{@"ryorder":self.checkBoxFootView.orderString},@{@"famxml":self.body}];
    [HttpTools postWithURL:KexchdataURL arrays:array method:KSetStaffListDeliVerMethod success:^(id JSON) {
       [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        self.rightItem.enabled = YES;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        GDataXMLElement *rootElement = [doc rootElement];
        NSArray *arrData= [rootElement elementsForName:@"info"];
        NSString * message= nil;
        for (GDataXMLElement * subXMLElement in arrData) {
            GDataXMLElement * element = [[subXMLElement elementsForName:@"msg"] objectAtIndex:0];
            message  = [element stringValue];
        }
    
            [self.view makeToast:@"发送成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOADetailData" object:nil];
            [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        [self.view makeToast:@"发送失败"];
         self.rightItem.enabled = YES;
    }];
    
    
}




#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groupArray.count;;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    OANameListModel * nameListModel = self.groupArray[section];
    NSArray * tempArray = nameListModel.subArray;
    return  nameListModel.isOpen ? tempArray.count : 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellIdentifier = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    OANameListModel * nameListModel = self.groupArray[indexPath.section];
    OASubNameListModel * subModel = nameListModel.subArray[indexPath.row];
    cell.textLabel.text = subModel.value;
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    if (subModel.isSelected) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HeaderView * headView = [HeaderView headerViewWithTableView:tableView];
    headView.delegate = self;
    headView.delegate2  = self;
    headView.groupModel= self.groupArray[section];
    return headView;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OANameListModel * nameListModel = self.groupArray[indexPath.section];
    OASubNameListModel * subModel = nameListModel.subArray[indexPath.row];
    subModel.isSelected = !subModel.isSelected;
    [self.tableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(void)clickHeaderView{
    [self.tableView reloadData];
}
-(void)clickHeaderView2{
    [self.tableView reloadData];
}

@end
